#ifndef _TIMER_H_
#define _TIMER_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>

#define time_before(a, b) \
	((a)->tv_sec < (b)->tv_sec || \
	 ((a)->tv_sec == (b)->tv_sec && (a)->tv_usec < (b)->tv_usec))

#define time_sub(a, b, res) do { \
	(res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
	(res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
	if ((res)->tv_usec < 0) { \
		(res)->tv_sec--; \
		(res)->tv_usec += 1000000; \
	} \
} while (0)

typedef void (*timeout_data_handler)(void *ctx);

int get_time(struct timeval *tm);

int register_timeout(long tv_secs, long tv_usecs,
					 timeout_data_handler handler,
					 void *ctx);
int cancel_timeout();

#endif	// _TIMER_H_
